﻿using System;
using System.Web;

namespace ART.Common
{
    /// <summary>
    /// Cookie操作类
    /// Create by Jinlong Zhang
    /// </summary>
    public class JCookie
    {
        private static readonly string IVKEY = "bp_cookie";
        /// <summary>
        /// 写入Cookie
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        ///<param name="cookieValue">天数</param>
        public static void WriteCookie(string cookieName, string cookieValue, double days)
        {
            if (GetCookieIsExist(cookieName))
            {
                RemoveCookie(cookieName);
            }
            cookieValue = JSecury.DesEncrypt(HttpContext.Current.Server.UrlEncode(cookieValue), IVKEY);
            System.Web.HttpCookie cookie = new HttpCookie(cookieName, cookieValue);

            cookie.Expires = DateTime.Now.AddDays(days);
            //cookie.Domain = ".SmartShop.com";
            cookie.Secure = false;
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

        }
        public static void WriteCookie(string cookieKey,string cookieName, string cookieValue, double days)
        {
            if (GetCookieIsExist(cookieKey,cookieName))
            {
                RemoveCookie(cookieKey,cookieName);
            }
            HttpCookie cookie;
            if (HttpContext.Current.Request.Cookies[cookieKey] == null)
            {
                cookie = new HttpCookie(cookieKey);
            }
            else
            {
                cookie = HttpContext.Current.Request.Cookies[cookieKey];
            }
            cookieValue = JSecury.DesEncrypt(HttpContext.Current.Server.UrlEncode(cookieValue), IVKEY);
            if (cookie.Values[cookieName] == null)
            {
                cookie.Values.Add(cookieName, cookieValue);
            }
            else
            {
                cookie.Values.Set(cookieName, cookieValue);
            }
            cookie.Expires = DateTime.Now.AddDays(days);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 移除Cookie
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        public static void RemoveCookie(string cookieName)
        {
            try
            {
                System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
                if (cookie!=null)
                {
                    cookie.Expires = DateTime.Now.AddDays(-5);
                    cookie.Values.Clear();
                    //System.Web.HttpContext.Current.Response.Cookies.Set(cookie);
                    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                }
            }
            catch
            {

            }

        }

        public static void RemoveCookie(string cookieKey, string cookieName)
        {
            try
            {
                System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
                if (cookie != null && cookie.Values[cookieName] != null)
                {
                    cookie.Expires = DateTime.Now.AddDays(-5);
                    cookie.Values.Clear();
                    //System.Web.HttpContext.Current.Response.Cookies.Set(cookie);
                    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                }
            }
            catch
            {

            }

        }

        /// <summary>
        /// 获取Cookie值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        public static string GetCookie(string cookieName)
        {
            System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
            string cookieValue = "";
            if (cookie != null)
            {
                if (cookieName == System.Web.Security.FormsAuthentication.FormsCookieName)
                {
                    System.Web.Security.FormsAuthenticationTicket ticket = System.Web.Security.FormsAuthentication.Decrypt(cookie.Value);
                    cookieValue = ticket.Name;
                }
                else
                    cookieValue = HttpContext.Current.Server.UrlDecode(JSecury.DesDecrypt(cookie.Value, IVKEY)).ToString();
            }
            return cookieValue;
        }

        public static string GetCookie(string cookieKey, string cookieName)
        {
            System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
            string cookieValue = "";
            if (cookie != null && cookie.Values[cookieName] != null)
            {
                cookieValue = HttpContext.Current.Server.UrlDecode(JSecury.DesDecrypt(cookie.Values[cookieName],IVKEY)).ToString();
            }
            return cookieValue;
        }



        /// <summary>
        /// 获取指定的Cookie是否存在
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <returns></returns>
        public static bool GetCookieIsExist(string cookieName)
        {
            //if (GetCookie(cookieName) == "")
            //    return false;
            //else
            //    return true;
            System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
            return cookie != null;
        }
        public static bool GetCookieIsExist(string cookieKey, string cookieName)
        {
            //if (GetCookie(cookieName) == "")
            //    return false;
            //else
            //    return true;
            System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
            return (cookie != null && cookie.Values[cookieName] != null);
        }
        /// <summary>
        /// 获取指定名称和值的Cookie是否存在
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        /// <returns></returns>
        public static bool GetCookieIsExistValue(string cookieName, string cookieValue)
        {
            string cookieValueSource = GetCookie(cookieName);
            if (cookieValueSource == "")
                return false;
            else
            {
                if (cookieValueSource == cookieValue)
                    return true;
                else
                    return false;
            }
        }
        public static bool GetCookieIsExistValue(string cookieKey, string cookieName, string cookieValue)
        {
            string cookieValueSource = GetCookie(cookieKey,cookieName);
            if (cookieValueSource == "")
                return false;
            else
            {
                if (cookieValueSource == cookieValue)
                    return true;
                else
                    return false;
            }
        }

    }
}
